1
/****************************** Module Header ******************************\
2 * Module Name: CpuUsageMonitorBase.cs
4 * Copyright (c) Microsoft Corporation.
6 * This is the base class of ProcessCpuUsageMonitor and TotalCpuUsageMonitor. It
7 * supplies basic fields/events/functions/interfaces of the monitors, such as Timer,
8 * PerformanceCounter and IDisposable interface.
11 * This source is subject to the Microsoft Public License.
12 * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
13 * All other rights reserved.
15 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
16 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
18 \***************************************************************************/
21 using System
.Collections
.Generic
;
22 using System
.Diagnostics
;
23 using System
.Threading
;
27 public abstract class CpuUsageMonitorBase
: IDisposable
29 object locker
= new object();
31 // Specify whether this instance is disposed.
32 bool disposed
= false;
34 // The timer is used to get the performance counter value every few seconds.
37 // The CPU usage performance counter will be initialized in ProcessCpuUsageMonitor
38 // and TotalCpuUsageMonitor.
39 protected PerformanceCounter cpuPerformanceCounter
= null;
41 // The max length of the CpuUsageValueArray.
42 int valueArrayCapacity
;
44 // The list is used to store the values.
45 List
<double> cpuUsageValueArray
;
48 /// Occurred if there is a new added value.
50 public event EventHandler
<CpuUsageValueArrayChangedEventArg
> CpuUsageValueArrayChanged
;
53 public event EventHandler
<ErrorEventArgs
> ErrorOccurred
;
56 /// Initialize the timer and performance counter.
58 /// <param name="timerPeriod">
59 /// If this value is no more than 0, then the timer will not be enabled.
61 /// <param name="valueArrayCapacity">
62 /// The max length of the CpuUsageValueArray.
64 /// <param name="categoryName">
65 /// The name of the performance counter category (performance object) with which
66 /// this performance counter is associated.
68 /// <param name="counterName">
69 /// The name of the performance counter.
71 /// <param name="instanceName">
72 /// The name of the performance counter category instance, or an empty string (""),
73 /// if the category contains a single instance.
75 public CpuUsageMonitorBase(int timerPeriod
, int valueArrayCapacity
,
76 string categoryName
, string counterName
, string instanceName
)
79 // Initialize the PerformanceCounter.
80 this.cpuPerformanceCounter
=
81 new PerformanceCounter(categoryName
, counterName
, instanceName
);
83 this.valueArrayCapacity
= valueArrayCapacity
;
84 cpuUsageValueArray
= new List
<double>();
89 // Delay the timer to invoke callback.
90 this.timer
= new Timer(new TimerCallback(Timer_Callback
),
91 null, timerPeriod
, timerPeriod
);
96 /// The method to be executed in the timer callback.
98 void Timer_Callback(object obj
)
104 if (this.cpuUsageValueArray
.Count
> this.valueArrayCapacity
)
106 this.cpuUsageValueArray
.Clear();
111 double value = GetCpuUsage();
122 this.cpuUsageValueArray
.Add(value);
124 double[] values
= new double[cpuUsageValueArray
.Count
];
125 cpuUsageValueArray
.CopyTo(values
, 0);
128 this.OnCpuUsageValueArrayChanged(
129 new CpuUsageValueArrayChangedEventArg() { Values = values }
);
133 this.OnErrorOccurred(new ErrorEventArgs { Error = ex }
);
139 /// Get current CPU usage.
141 protected virtual double GetCpuUsage()
143 if (!IsInstanceExist())
145 throw new ApplicationException(
146 string.Format("The instance {0} is not available. ",
147 this.cpuPerformanceCounter
.InstanceName
));
151 double value = cpuPerformanceCounter
.NextValue();
156 /// Child class may override this method to determine whether the instance exists.
158 /// <returns></returns>
159 protected virtual bool IsInstanceExist()
165 /// Raise the CpuUsageValueArrayChanged event.
167 protected virtual void OnCpuUsageValueArrayChanged(CpuUsageValueArrayChangedEventArg e
)
169 if (this.CpuUsageValueArrayChanged
!= null)
171 this.CpuUsageValueArrayChanged(this, e
);
176 /// Raise the ErrorOccurred event.
178 protected virtual void OnErrorOccurred(ErrorEventArgs e
)
180 if (this.ErrorOccurred
!= null)
182 this.ErrorOccurred(this, e
);
186 // Release the resources.
187 public void Dispose()
190 GC
.SuppressFinalize(this);
193 protected virtual void Dispose(bool disposing
)
195 // Protect from being called multiple times.
208 if (cpuPerformanceCounter
!= null)
210 cpuPerformanceCounter
.Dispose();